home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / Curl.php < prev    next >
PHP Script  |  2004-03-24  |  9KB  |  427 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Sterling Hughes <sterling@php.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Curl.php,v 1.5 2003/05/15 23:43:54 sterling Exp $
  20. //
  21. // A nice friendly OO interface for CURL
  22. //
  23. require_once('PEAR.php');
  24.  
  25. class Net_Curl 
  26. {
  27.     // {{{ Public Properties
  28.     
  29.     /**
  30.      * The URL for cURL to work with
  31.      *
  32.      * @var string $url
  33.      * @access public
  34.      */
  35.     var $url;
  36.     /**
  37.      * The Username for standard HTTP Authentication
  38.      *
  39.      * @var string $username
  40.      * @access public
  41.      */
  42.     var $username="";
  43.     
  44.         /**
  45.      * The Password for standard HTTP Authentication
  46.      *
  47.      * @var string $password
  48.      * @access public
  49.      */
  50.     var $password="";    
  51.     
  52.         /**
  53.      * The SSL version for the transfer
  54.      *
  55.      * @var integer $sslVersion
  56.      * @access public
  57.      */
  58.     var $sslVersion;
  59.     
  60.     /**
  61.      * The filename of the SSL certificate
  62.      *
  63.      * @var string $sslCert
  64.      * @access public
  65.      */
  66.     var $sslCert;
  67.     
  68.     /**
  69.      * The password corresponding to the certificate
  70.      * in the $sslCert property
  71.      *
  72.      * @var string $sslCertPasswd
  73.      * @access public
  74.      */
  75.     var $sslCertPasswd;
  76.  
  77.     /**
  78.      * User Agent string when making an HTTP request
  79.      *
  80.      * @var string $userAgent
  81.      * @access public
  82.      */
  83.     var $userAgent;
  84.     
  85.     /**
  86.      * Whether or not to include the header in the results
  87.      * of the CURL transfer
  88.      *
  89.      * @var boolean $header
  90.      */
  91.     var $header = 0;
  92.     
  93.     /**
  94.      * Whether or not to output debug information while executing a
  95.      * curl transfer
  96.      *
  97.      * @var boolean $verbose
  98.      * @access public
  99.      */
  100.     var $verbose = 0;
  101.     
  102.     /**
  103.      * Whether or not to display a progress meter for the current transfer
  104.      *
  105.      * @var boolean $progress
  106.      * @access public
  107.      */
  108.     var $progress = 0;
  109.     
  110.     /**
  111.      * Whether or not to suppress error messages
  112.      *
  113.      * @var boolean $mute
  114.      * @access public
  115.      */
  116.     var $mute = 1;
  117.     
  118.     /**
  119.      * Whether or not to follow HTTP Location headers.
  120.      *
  121.      * @var boolean $follow_location
  122.      * @access public
  123.      */
  124.     var $follow_location = 1;
  125.  
  126.     /**
  127.      * Time allowed for current transfer, in seconds.  0 means no limit
  128.      *
  129.      * @var int $timeout
  130.      * @access public
  131.      */
  132.     var $timeout = 0;
  133.  
  134.     /**
  135.      * Whether or not to return the results of the
  136.      * current transfer
  137.      *
  138.      * @var boolean $return_transfer
  139.      * @access public
  140.      */
  141.     var $return_transfer = 1;
  142.     
  143.     /**
  144.      * The type of transfer to perform
  145.      *
  146.      * @var string $type
  147.      * @access public
  148.      */
  149.     var $type;
  150.     
  151.     /**
  152.      * The file to upload
  153.      *
  154.      * @var string $file
  155.      * @access public
  156.      */
  157.     var $file;
  158.     
  159.     /**
  160.      * The file size of the file pointed to by the $file
  161.      * property
  162.      *
  163.      * @var integer $file_size
  164.      * @access public
  165.      */
  166.     var $file_size;
  167.     
  168.     /**
  169.      * The cookies to send to the remote site
  170.      *
  171.      * @var array $cookies
  172.      * @access public
  173.      */
  174.     var $cookies;
  175.     
  176.     /**
  177.      * Additional HTTP headers to send to the remote site
  178.      *
  179.      * @var array $http_headers
  180.      * @access public
  181.      */
  182.     var $http_headers;
  183.     
  184.     /**
  185.      * The fields to send in a 'POST' request
  186.      *
  187.      * @var array $fields
  188.      * @access public
  189.      */
  190.     var $fields;
  191.     
  192.     /**
  193.      * The proxy server to go through
  194.      *
  195.      * @var string $proxy
  196.      * @access public
  197.      */
  198.     var $proxy;
  199.     
  200.     /**
  201.      * The username for the Proxy server
  202.      *
  203.      * @var string $proxyUser
  204.      * @access public
  205.      */
  206.     var $proxyUser;
  207.     
  208.     /**
  209.      * The password for the Proxy server
  210.      *
  211.      * @var string $proxyPassword
  212.      * @access public
  213.      */
  214.     var $proxyPassword;
  215.     
  216.     // }}}
  217.     // {{{ Private Properties
  218.     
  219.     /**
  220.      * The current curl handle
  221.      *
  222.      * @var resource $_ch
  223.      * @access public
  224.      */
  225.     var $_ch;
  226.     
  227.     // }}}
  228.     // {{{ Net_Curl()
  229.     
  230.     /**
  231.      * The Net_Curl constructor, called when a new Net_Curl object
  232.      * is initialized
  233.      *
  234.      * @param string           [$url] The URL to fetch (can be set 
  235.      *                                using the $url property as well)
  236.      *
  237.      * @return object Net_Curl $obj   A new Net_Curl object
  238.      *
  239.      * @access public
  240.      * @author Sterling Hughes <sterling@php.net>
  241.      * @since  PHP 4.0.5
  242.      */
  243.     function Net_Curl($url = "")
  244.     {
  245.         if ($url) {
  246.             $this->url = $url;
  247.         }
  248.         
  249.         $ch = curl_init();
  250.         if (!$ch) {
  251.             $this = new PEAR_Error("Couldn't initialize a new curl handle");
  252.         }
  253.         
  254.         $this->_ch = $ch;
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ execute()
  259.     
  260.     /**
  261.      * Executes a prepared CURL transfer
  262.      *
  263.      * @access public
  264.      * @author Sterling Hughes <sterling@php.net>
  265.      * @since  PHP 4.0.5
  266.      */
  267.     function execute()
  268.     {
  269.         $ch  = &$this->_ch;
  270.         $ret = true;
  271.         // Basic stuff
  272.         
  273.         $ret = curl_setopt($ch, CURLOPT_URL,    $this->url);
  274.         $ret = curl_setopt($ch, CURLOPT_HEADER, $this->header);
  275.         
  276.         // Whether or not to return the transfer contents
  277.         if ($this->return_transfer && !isset($this->file)) {
  278.             $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  279.         }
  280.         
  281.         // HTTP Authentication
  282.         
  283.         if ($this->username != "") {
  284.             $ret = curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
  285.         }
  286.         
  287.         // SSL Checks
  288.         
  289.         if (isset($this->sslVersion)) {
  290.             $ret = curl_setopt($ch, CURLOPT_SSLVERSION, $this->sslVersion);
  291.         }
  292.         
  293.         if (isset($this->sslCert)) {
  294.             $ret = curl_setopt($ch, CURLOPT_SSLCERT, $this->sslCert);
  295.         }
  296.         
  297.         if (isset($this->sslCertPasswd)) {
  298.             $ret = curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $this->sslCertPasswd);
  299.         }
  300.         
  301.         // Proxy Related checks
  302.  
  303.         if (isset($this->proxy)) {
  304.             $ret = curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
  305.         }
  306.  
  307.         if (isset($this->proxyUser) || isset($this->proxyPassword)) {
  308.             $proxyString = $this->proxyUser . ":" . $this->proxyPassword;
  309.             
  310.             $ret = curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyString);
  311.         }
  312.         
  313.         
  314.         // Transfer type
  315.         
  316.         if (isset($this->type)) {
  317.             switch (strtolower($this->type)) {
  318.             case 'post':
  319.                 $ret = curl_setopt($ch, CURLOPT_POST, 1);
  320.                 break;
  321.             case 'put':
  322.                 $ret = curl_setopt($ch, CURLOPT_PUT, 1);
  323.                 break;
  324.             }
  325.         }
  326.         
  327.         
  328.         // Transfer upload, etc. related
  329.         
  330.         if (isset($this->file)) {
  331.             if (!isset($this->file_size)) {
  332.                 $this->file_size = filesize($this->file);
  333.             }
  334.             
  335.             $ret = curl_setopt($ch, CURLOPT_INFILE, $this->file);
  336.             $ret = curl_setopt($ch, CURLOPT_INFILESIZE, $this->file_size);
  337.         }
  338.         
  339.         if (isset($this->fields)) {
  340.             if (!isset($this->type)) {
  341.                 $this->type = 'post';
  342.                 $ret = curl_setopt($ch, CURLOPT_POST, 1);
  343.             }
  344.             
  345.             $ret = curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields);
  346.         }
  347.         
  348.         
  349.         // Error related
  350.         
  351.         if ($this->progress) {
  352.             $ret = curl_setopt($ch, CURLOPT_PROGRESS, 1);
  353.         }
  354.         
  355.         if ($this->verbose) {
  356.             $ret = curl_setopt($ch, CURLOPT_VERBOSE, 1);
  357.         }
  358.         
  359.         if (!$this->mute) {
  360.             $ret = curl_setopt($ch, CURLOPT_MUTE, 0);
  361.         }
  362.  
  363.  
  364.         // Other stuff
  365.  
  366.         $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
  367.  
  368.         if ($this->timeout) {
  369.             $ret = curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  370.         }
  371.  
  372.         if (isset($this->userAgent)) {
  373.             $ret = curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
  374.         }
  375.         
  376.  
  377.         // Cookies
  378.         
  379.         if (isset($this->cookies)) {
  380.             foreach ($this->cookies as $name => $value) {
  381.                 $cookie_data .= urlencode($name) . '=' . urlencode($value) . ';';
  382.             }
  383.         
  384.             $ret = curl_setopt($ch, CURLOPT_COOKIE, $cookie_data);
  385.         }
  386.         
  387.  
  388.         // Other HTTP headers
  389.  
  390.         if (isset($this->http_headers)) {
  391.             $ret = curl_setopt($ch, CURLOPT_HTTPHEADER, $this->http_headers );
  392.         }
  393.  
  394.  
  395.         $ret = curl_exec($ch);
  396.         if (!$ret) {
  397.             $errObj = new PEAR_Error(curl_error($ch), curl_errno($ch));
  398.             return($errObj);
  399.         }
  400.         
  401.         return($ret);
  402.     }
  403.     
  404.     // }}}
  405.     // {{{ close()
  406.     
  407.     /**
  408.      * Closes the curl transfer and finishes the object (kinda ;)
  409.      *
  410.      * @access public
  411.      * @author Sterling Hughes <sterling@php.net>
  412.      * @since  PHP 4.0.5
  413.      */
  414.     function close()
  415.     {
  416.         if ($this->_ch) {
  417.             curl_close($this->_ch);
  418.         }
  419.     }
  420.     
  421.     // }}}
  422. }
  423.  
  424. // }}}
  425.  
  426. ?>
  427.